from OWRpy import *
import os.path, redREnviron
import redRi18n
_ = redRi18n.get_(package = 'base')
import signals
import redRGUI


class ecoHclust(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["hclust"])
        self.data = {}
        self.RFunctionParam_d = ''
        self.inputs.addInput('id0', 'd', signals.base.RDataFrame, self.processd) # here should design dissimilarity signal

        self.outputs.addOutput('id0', 'hclust Output', signals.base.RList)

        
        self.RFunctionParam_clustMethod = redRGUI.base.comboBox(self.controlArea, label = "Cluster Method:", items = ["complete","ward","single","average","mcquitty","centroid"])
        
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processd(self, data):
        if not self.require_librarys(["stats"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_d=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_d=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_d) == '': 
            self.status.setText('No data to process')
            return
        injection = []
        string = 'method=\''+unicode(self.RFunctionParam_clustMethod.currentText())+'\''
        injection.append(string)
        inj = ','.join(injection)
        self.R(self.Rvariables['hclust']+'<-hclust(d='+unicode(self.RFunctionParam_d)+','+inj+')')
        newData = signals.base.RList(self, data = self.Rvariables["hclust"], checkVal = False) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        #newData.copyAllOptinoalData(self.data)  ## note, if you plan to uncomment this please uncomment the call to set self.data in the process statemtn of the data whose attributes you plan to send forward.
        self.rSend("id0", newData)
